package app.modules.security.util.conversion;

import app.modules.common.annotation.ConvertFrom;
import app.modules.common.annotation.ConvertTo;
import app.modules.security.dto.web.resource.WebResourceDTO;
import cn.hutool.core.collection.CollectionUtil;
import jasmine.framework.common.util.MapperUtil;
import jasmine.security.rbac.model.SecResource;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author mh.z
 */
public class ResourceConvertUtil {

    /**
     * 转换成 WebResourceDTO 列表
     *
     * @param resources
     * @return
     */
    @ConvertFrom(SecResource.class)
    @ConvertTo(WebResourceDTO.class)
    public static List<WebResourceDTO> secResource2WebResourceDTO(Collection<SecResource> resources) {
        if (CollectionUtil.isEmpty(resources)) {
            return Collections.emptyList();
        }

        List<WebResourceDTO> webResourceDtoList = MapperUtil.mapToList(resources, WebResourceDTO.class);
        return webResourceDtoList;
    }

    /**
     * 转换成 WebResourceDTO 对象
     *
     * @param resource
     * @return
     */
    @ConvertFrom(SecResource.class)
    @ConvertTo(WebResourceDTO.class)
    public static WebResourceDTO secResource2WebResourceDTO(SecResource resource) {
        if (resource == null) {
            return null;
        }

        return CollectionUtil.getFirst(secResource2WebResourceDTO(Collections.singleton(resource)));
    }

}
